#set(subTable = subTables.get(subIndex)) ###当前表
#set(subColumns = subColumnsList.get(subIndex)) ###当前字段数组
#set(subJoinColumn = subJoinColumns.get(subIndex))###当前 join 字段
#set($SubJoinColumnName = upperFirst(subJoinColumn.javaField))###首字母大写
#set(tableComment = subTable.tableComment)
#set(entityClass = subTable.entityClass())
#set(entityProxyClass = subTable.entityProxyClass())
#set(entityVar = subTable.entityVar())
#set(baseServiceClass = subTable.baseServiceClass())
#set(serviceClass = subTable.serviceClass())
package #(basePackage).#(table.moduleType).#(table.moduleName).#(table.domainName).service;

import com.easy.query.core.api.pagination.EasyPageResult;
import #(PageResultClassName);
import #(PageParamClassName);
#if(table.templateType == 2)
import #(TreeUtils);
#end
import com.goldsyear.dolphin.framework.module.web.core.service.BaseService;

import #(basePackage).#(table.moduleType).#(table.moduleName).#(table.domainName).entity.#(entityClass);
import #(basePackage).#(table.moduleType).#(table.moduleName).#(table.domainName).entity.proxy.#(entityProxyClass);

import java.util.*;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.Component;

/**
 * #(tableComment) Service 服务层实现类
 *
 * @author #(table.author)
 */
@Component
@Slf4j
public class #(serviceClass) extends #(baseServiceClass)<#(entityClass), #(entityProxyClass)> {

    public #(serviceClass)() {
        super(#(entityClass).class, #(entityProxyClass).class);
    }

    public long saveOrUpdate(#(entityClass) entity) {
        return entityInsertable(entity)
            .onConflictThen(proxy -> proxy.FETCHER.allFields())
            .executeRows(true);
    }

    public PageResult<#(entityClass)> paginateBy#(SubJoinColumnName)(PageParam pageParam, #(subJoinColumn.javaType) #(subJoinColumn.javaField)) {
        EasyPageResult<#(entityClass)> pageResult = entityQueryable()
                .where(proxy -> proxy.#(subJoinColumn.javaField)().eq(#(subJoinColumn.javaField)))
                .orderBy(proxy -> proxy.id().desc())
                .toPageResult(pageParam.getPageNo(), pageParam.getPageSize());
        return new PageResult<>(pageResult.getData(), pageResult.getTotal());
    }

    public List<#(entityClass)> listBy#(SubJoinColumnName)(#(subJoinColumn.javaType) #(subJoinColumn.javaField)) {
        return entityQueryable()
                .where(proxy -> proxy.#(subJoinColumn.javaField)().eq(#(subJoinColumn.javaField)))
                .orderBy(proxy -> proxy.id().desc())
                .toList();
    }

    
    public #(entityClass) getBy#(SubJoinColumnName)(#(subJoinColumn.javaType) #(subJoinColumn.javaField)) {
        return entityQueryable()
                .where(proxy -> proxy.#(subJoinColumn.javaField)().eq(#(subJoinColumn.javaField)))
                .firstOrNull();
    }

    public long deleteBy#(SubJoinColumnName)(#(subJoinColumn.javaType) #(subJoinColumn.javaField)) {
        return entityDeletable()
                .where(proxy -> proxy.#(subJoinColumn.javaField)().eq(#(subJoinColumn.javaField)))
                .executeRows();
    }
}